package com.atf.atfconfigurator.views;

import java.util.Iterator;

import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.atf.atfconfigurator.utils.WasConstant;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class ListenPortView extends VerticalLayout implements View{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private PropertiesConfiguration wasConfigProperty;
	
	@Override
	public void enter(ViewChangeEvent event) {
		
		// TODO Auto-generated method stub
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();
		setSpacing(true);
        setMargin(true);
        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
        final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
        final TextField noOfLPs = new TextField("Number of LPs");
	     int noOfLPsValue=wasConfigProperty.getInt("noOfLPs",0);
	     noOfLPs.setValue(String.valueOf(noOfLPsValue));
	     noOfLPs.setWidth("50%");
	     form.addComponent(noOfLPs);
	     
	     noOfLPs.addValueChangeListener(new Property.ValueChangeListener(){

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void valueChange(ValueChangeEvent event) {
					// TODO Auto-generated method stub
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("noOfLPs",  (String) event.getProperty().getValue());
					((AtfconfiguratorUI) getUI()).navigator.navigateTo("ListenPort");
				}        	
	        });
	     if(noOfLPsValue > 0){
	    	 for(int i=1; i<noOfLPsValue+1; i++){
	    		 form.addComponent(new CollapseableBox(String.valueOf(i),getOfLPsEntries(i), false));
	    	 }
	     }
	}

	private Component getOfLPsEntries(final int pos) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light"); 
        HierarchicalConfiguration sub = WasConstant.getWasConstantFile().configurationAt("listenerPorts");
        Iterator iterator=sub.getKeys();
        while(iterator.hasNext()){
        	String key=(String) iterator.next();
        	String value=sub.getString(key);
        	if( wasConfigProperty.containsKey("lp"+pos+key)){
    			value=wasConfigProperty.getString("lp"+pos+key);
    		}else{
    			((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("lp"+pos+key,value);
    		}
        	if(key.equalsIgnoreCase("ConnectionFactoryJNDIName")){
        		NativeSelect qCFsJNDINameField=getQCFsJNDIName();
    			qCFsJNDINameField.setValue(value);
    			qCFsJNDINameField.addValueChangeListener(new Property.ValueChangeListener(){

    				@Override
    				public void valueChange(ValueChangeEvent event) {
    					// TODO Auto-generated method stub
    					String value = (String) event.getProperty().getValue();
    					
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("lp"+pos+"ConnectionFactoryJNDIName", value);
    					
    				}
                	
                });
    			qCFsJNDINameField.setImmediate(true);
                form.addComponent(qCFsJNDINameField);
        	}else if(key.equalsIgnoreCase("DestinationJNDIName")){
        		NativeSelect dDsJNDINameField=getQDsJNDIName();
        		dDsJNDINameField.setValue(value);
    			dDsJNDINameField.addValueChangeListener(new Property.ValueChangeListener(){

    				@Override
    				public void valueChange(ValueChangeEvent event) {
    					// TODO Auto-generated method stub
    					String value = (String) event.getProperty().getValue();
    					
    						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("lp"+pos+"DestinationJNDIName", value);
    					
    				}
                	
                });
    			dDsJNDINameField.setImmediate(true);
                form.addComponent(dDsJNDINameField);
        	}else{
	    		final TextField parNameText = new TextField(key);
	    		parNameText.setValue(value);
	    		parNameText.setWidth("100%");
	    		parNameText.addValueChangeListener(new Property.ValueChangeListener(){
	
					@Override
					public void valueChange(ValueChangeEvent event) {
						// TODO Auto-generated method stub
						String value = (String) event.getProperty().getValue();
						((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("lp"+pos+parNameText.getCaption(), value);
					}
	            	
	            });
	    		parNameText.setImmediate(true);
	            form.addComponent(parNameText);
        	}
        }
		return form;
	}
	private NativeSelect getQDsJNDIName() {
		NativeSelect qdjndi = new NativeSelect("QD JNDI");
		qdjndi.setWidth("50%");
		qdjndi.setNullSelectionAllowed(false);
		int numberOfnoOfQCFss=wasConfigProperty.getInt("noOfQDs", 0);
		for(int i=1; i<numberOfnoOfQCFss+1; i++){
	    	 String name =wasConfigProperty.getString("qd"+1+"JNDIName");
	    	 qdjndi.addItem(name);
        }
		
	    return qdjndi;
	}

	private NativeSelect getQCFsJNDIName(){
		NativeSelect qcfjndi = new NativeSelect("QCF JNDI");
		qcfjndi.setWidth("50%");
		qcfjndi.setNullSelectionAllowed(false);
		int numberOfnoOfQCFss=wasConfigProperty.getInt("noOfQCFs", 0);
		for(int i=1; i<numberOfnoOfQCFss+1; i++){
	    	 String name =wasConfigProperty.getString("qcf"+1+"JNDIName");
	    	 qcfjndi.addItem(name);
        }
		
	    return qcfjndi;
		
	}

}
